using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;


namespace DataSchema
{
    public static class DataMgr
    {
        public const string EXTDS = ".shc";
        public const string EXTOBJ = ".shco";
        public static void Serialize(CollectionSchema sch, string path)
        {
            DataModel.Collection col = DataModel.Collection.CreateCollection(((CollectionSchema.CollectionRow)(sch.Collection.Rows[0])).Name);
            col.Path = ((CollectionSchema.CollectionRow)(sch.Collection.Rows[0])).Path;
            col.CSVPath = ((CollectionSchema.CollectionRow)(sch.Collection.Rows[0])).CSVFolderPath;
            col.Description = ((CollectionSchema.CollectionRow)(sch.Collection.Rows[0])).Description;

            foreach (DataSchema.CollectionSchema.ShotRow re in sch.Shot.Rows)
            {
                //Model.ModelMgr.Add(re.Name);

                /*
                DataModel.Entity ent = new DataSchema.DataModel.Entity(re.Name);
                
                if(!re.IsSelectedNull())
                    ent.Selected = re.Selected;

                foreach (DataSchema.CollectionSchema.ShotRow sr in CollectionSchema.GetEntityShots(re))
                {
                    DataModel.Shot shot = new DataSchema.DataModel.Shot(sr.Name);
                    
                    if(!sr.IsSSNull())
                        shot.SS = sr.SS;
                    if (!sr.IsHSNull())
                        shot.HS = sr.HS;
                    if (!sr.IsSLNull())
                        shot.SL = sr.SL;
                    if (!sr.IsHLNull())
                        shot.HL = sr.HL;
                    if (!sr.IsSBNull())
                        shot.SB = sr.SB;
                    if (!sr.IsHBNull())
                        shot.HB = sr.HB;

                    foreach (DataSchema.CollectionSchema.ItemRow ir in CollectionSchema.GetShotItems(sr))
                    {
                        DataModel.Item item = new DataSchema.DataModel.Item(ir.Name);
                        shot.Items.Add(item);
                    }
                    ent.Shots.Add(shot);
                }
                col.Entities.Add(ent);
            }
            
            XmlSerializer x = new XmlSerializer(typeof(DataModel.Collection));

            using (TextWriter writer = new StreamWriter(path))
            {
                x.Serialize(writer, col);
                writer.Close();
            }
             */
            }
            Model.ModelMgr.Save();
        }
    }
}
